package calendar;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

/* this class provides a panel to display a list of users (calendars) */
class UserListPanel extends CalendarPanel implements ItemListener {

  private static final long serialVersionUID = 34398493L;

  ArrayList users;
  ArrayList selectedUsers;
  HashMap userMap;
  HashMap userMapCopy;
  JLabel title;

  public UserListPanel()
  {
    users = new ArrayList();
    selectedUsers = new ArrayList();
    userMap = new HashMap();

    title = new JLabel( " Current Users [User Name (User ID)]:" );
    title.setAlignmentX( TOP_ALIGNMENT );

    setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );

    add( title );
  }

  /* update the list of users in the panel */
  public void setUsers( ArrayList users )
  {
    this.users = users;
    this.removeAll();
   // userMapCopy = (HashMap)userMap.clone();
    userMap.clear();

    add( title );

    Iterator itr1 = users.iterator();
    while ( itr1.hasNext() )
    {
      User user = (User)itr1.next();
      String userName = user.getName() + " ( " + user.getId() + " )";
      JCheckBox checkBox = new JCheckBox( userName );
      userMap.put( user, checkBox );
      add( (JCheckBox)userMap.get( user ) );
      ((JCheckBox)userMap.get( user )).addItemListener( this);
    //  if ( !userMapCopy.isEmpty() && ((JCheckBox)userMapCopy.get( user )).isSelected() )
    //    ((JCheckBox)userMap.get( user )).setSelected( true );
    }

    this.updateUI();
  }

  public int getUsersSize()
  {
    return users.size();
  }

  public ArrayList getUsers()
  {
    return users;
  }

  public void setUserChecked( User user )
  {
    Iterator itr = users.iterator();
    while ( itr.hasNext() )
    {
      User u = (User)itr.next();
      if ( u.getId() == user.getId() )
      {
        ((JCheckBox)userMap.get( u )).setSelected( true );
      }
    }

    int eventId = CalEvent.LIST_EVENTS;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public ArrayList getSelectedUsers()
  {
    return selectedUsers;
  }

  /* clear the checkboxes next to all the users */
  public void clearSelectedUsers()
  {
    Iterator itr = users.iterator();
    while ( itr.hasNext() )
    {
      User user = (User)itr.next();
      ((JCheckBox)userMap.get( user )).setSelected( false );
    }
    selectedUsers.clear();
  }

  /* determine action based on user boxes checked/unchecked */
  public void itemStateChanged( ItemEvent e )
  {
    int eventId = -1;

    Object source = e.getItemSelectable();

    Iterator itr = users.iterator();
    while ( itr.hasNext() )
    {
      User user = (User)itr.next();
      if ( source == (JCheckBox)userMap.get( user ) )
      {
        if ( e.getStateChange() == ItemEvent.SELECTED )
        {
          selectedUsers.add( user );
        }
        else if ( e.getStateChange() == ItemEvent.DESELECTED )
        {
          selectedUsers.remove( user );
        }
      }
    }

    eventId = CalEvent.LIST_EVENTS;
    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }

  public void actionPerformed(ActionEvent e) {

    int eventId = -1;

    CalEvent calEvent = new CalEvent( this, eventId );
    fireCalEvent( calEvent );
  }
}
